package homework2.client;

import homework2.common.Message;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

/**
 * 客户端业务处理类
 *
 * @author jian
 * @date 2024/01/08 19:09
 **/
public class Client {
    String id;

    public static final String recDirPath = "D:\\java\\program\\study\\集训\\parent\\javav2.0\\src\\main\\java\\homework2\\client\\files\\";
    Client(String id){
        this.id = id;
    }
    public void  start(){
        Scanner sc = new Scanner(System.in);
        boolean loop = true;
        try {
            Socket socket = new Socket(Properties.ADDRESS, Properties.PORT);
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            Message idMessage = new Message();
            idMessage.setUserId(id);
            oos.writeObject(idMessage);
            ClientConnectServerThread clientConnectServerThread = new ClientConnectServerThread(socket);
            clientConnectServerThread.start();
            while (loop) {
                menu(socket);
            }

        } catch (IOException e) {
            System.out.println("发送客户端id出错");;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public static void menu(Socket socket) throws IOException, ClassNotFoundException {
        boolean loop = true;
        Scanner sc = new Scanner(System.in);
        while (loop) {
            System.out.println("\n=========菜单=======");
            System.out.println("\t\t 1 显示在线用户列表");
            System.out.println("\t\t 2 命令窗口");
            System.out.println("\t\t 3 退出");
            switch (sc.next()){
                case "1":
                    ClientService.getOnlineList(socket);
                    break;
                case "2":
                    ClientService.handleCommand(socket);
                    break;
                case "3":
                    loop = false;
                    break;
                default:
                    System.out.println("指令不存在请重新输入");
            }
        }
    }
}
